package com.ydd.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ydd.dto.SYSUserDTO;
import com.ydd.dto.SYSUserPageQueryDTO;
import com.ydd.entity.SysUser;
import com.ydd.mapper.SysUserMapper;
import com.ydd.result.PageResult;
import com.ydd.service.SysUserService;
import com.ydd.vo.SysUserPage;
import com.ydd.vo.SysUserPageVO;
import com.ydd.vo.SysUserVO;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

/**
 * @Description TODO
 * @Author fang
 * @Date 2023-05-29 16:22
 */
@Service
public class SysUserServiceImpl implements SysUserService {
    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 新增管理员
     *
     * @param sysUserDTO
     */
    @Override
    public void userAdd(SYSUserDTO sysUserDTO) {
        //转为user对象
        SysUser sysUser = new SysUser();
        //将dto配置到user类中
        BeanUtils.copyProperties(sysUserDTO, sysUser);
        //添加创建时间
        sysUser.setCreateDate(LocalDateTime.now());
        //密码使用md5加盐加密
        sysUser.setPassword(DigestUtils.md5DigestAsHex((sysUserDTO.getPassword()).getBytes()));
        //生成uuid
        UUID uuid = UUID.randomUUID();
        String id = uuid.toString();
        sysUser.setId(id);
        sysUser.setRoleId(sysUserDTO.getRole_id().toString().replaceAll(" ", ""));
        sysUserMapper.userAdd(sysUser);

    }

    /**
     * 管理员分页
     *
     * @param sysUserPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(SYSUserPageQueryDTO sysUserPageQueryDTO) {
        Integer page = Integer.parseInt(sysUserPageQueryDTO.getPage());
        Integer pageSize = Integer.parseInt(sysUserPageQueryDTO.getLimit());
        PageHelper.startPage(page, pageSize);
        //用户数据集合
        Page<SysUserPage> sysUserList = sysUserMapper.Pagedad(sysUserPageQueryDTO);
        //循环遍历每个用户对象 将roleid字符串转换成roleid集合
        for (SysUserPage sysUser : sysUserList) {
            String roleId = sysUser.getRoleIdStr();
            String[] roleNameId = roleId.replaceAll("[\\[\\]\"]", "").split(",");
            List<String> rolename = sysUserMapper.getrole(roleNameId);
            sysUser.setRoleId(Arrays.asList(roleNameId));
            sysUser.setRoleName(rolename);
        }


        return new PageResult<>(sysUserList.getTotal(), sysUserList.getPages(), sysUserList.getPageSize(), page, sysUserList.getResult());
    }

    /**
     * 管理员删除
     *
     * @param id
     */
    @Override
    public void del(String id) {
        sysUserMapper.del(id);
    }

    /**
     * 管理员详情
     *
     * @param id
     * @return
     */
    @Override
    public SysUserVO getUser(String id) {
        SysUserVO user = sysUserMapper.getUser(id);
        SysUser sysUser = sysUserMapper.selectById(id);
        String[] roleIds = sysUser.getRoleId().replaceAll("[\\[\\]\"]", "").split(",");
        List<String> role_id = Arrays.asList(roleIds);
        user.setRole_id(role_id);
        user.setPassword("");
        return user;
    }

    /**
     * 管理员修改
     *
     * @param sysUserDTO
     */
    @Override
    public void upUser(SYSUserDTO sysUserDTO) {
        SysUser sysUser = new SysUser();
        BeanUtils.copyProperties(sysUserDTO, sysUser);
        if (sysUserDTO.getPassword() != null && sysUserDTO.getPassword().length() != 0) {
            sysUser.setPassword(DigestUtils.md5DigestAsHex((sysUserDTO.getPassword()).getBytes()));
        }
        if (sysUser.getName()!=null){
            sysUser.setRoleId(sysUserDTO.getRole_id().toString().replaceAll(" ",""));
        }
        sysUser.setId(sysUserDTO.getId());
        sysUser.setUpdateDate(LocalDateTime.now());
        if (sysUser.getStatus()==2){
            sysUser.setStatus(0);
        }
        sysUserMapper.upUser(sysUser);
    }
}
